﻿#region Imports
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using CommonUtilities.Extensions;
#endregion

namespace CommonUtilities.Util
{
    public static class ScreenshotUtil
    {
        /*
         * http://msdn.microsoft.com/en-us/library/ms633519%28v=vs.85%29.aspx 
         * http://stackoverflow.com/questions/9668872/how-to-get-windows-position 
         * http://stackoverflow.com/questions/1434524/how-do-i-find-the-position-location-of-a-window-given-a-hwnd-without-nativemet/1461322#1461322 
         * http://stackoverflow.com/questions/10233055/how-to-get-screenshot-to-include-the-invoking-window-on-xp?rq=1 
         * http://stackoverflow.com/questions/16633196/c-sharp-how-to-get-a-screenshot-of-a-window?rq=1 
         * http://stackoverflow.com/questions/4754197/get-image-of-any-window-without-taking-a-screen-shot?rq=1 
         * http://stackoverflow.com/questions/4176340/how-to-take-a-screenshot-of-a-full-size-window-in-c-sharp?rq=1
        */



        #region Methods
        public static void TakeScreenshot(Form frm)
        {
            //* <-- http://stackoverflow.com/questions/5325168/how-to-take-screen-shot-of-mdi-form-while-clicking-on-any-toolstrip-menu-item?rq=1 -->
            Bitmap screenshot = new Bitmap(frm.Width, frm.Height);
            using (Graphics g = Graphics.FromImage(screenshot))
            {
                g.CopyFromScreen(0, 80, 0, 0, screenshot.Size);
            }
            //*/


            /* <-- http://stackoverflow.com/questions/10233055/how-to-get-screenshot-to-include-the-invoking-window-on-xp?rq=1 -->
            Size sz = Screen.PrimaryScreen.Bounds.Size;
            IntPtr hDesk = GetDesktopWindow();
            IntPtr hSrce = GetWindowDC(hDesk);
            IntPtr hDest = CreateCompatibleDC(hSrce);
            IntPtr hBmp = CreateCompatibleBitmap(hSrce, sz.Width, sz.Height);
            IntPtr hOldBmp = SelectObject(hDest, hBmp);
            bool b = BitBlt(hDest, 0, 0, sz.Width, sz.Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);
            Bitmap bmp = Bitmap.FromHbitmap(hBmp);
            SelectObject(hDest, hOldBmp);
            DeleteObject(hBmp);
            DeleteDC(hDest);
            ReleaseDC(hDesk, hSrce);
            bmp.Save(@"c:\temp\test.png");
            bmp.Dispose();
            */


            /* <-- http://stackoverflow.com/questions/10233055/how-to-get-screenshot-to-include-the-invoking-window-on-xp?rq=1 -->
            // TAKES SCREENSHOT OF WHOLE MONITOR
            int ssX, ssY, ssWidth, ssHeight;

            ssX = Screen.PrimaryScreen.Bounds.X;
            ssY = Screen.PrimaryScreen.Bounds.Y;
            ssWidth = Screen.PrimaryScreen.Bounds.Width;
            ssHeight = Screen.PrimaryScreen.Bounds.Height;
            Size ssSize = Screen.PrimaryScreen.Bounds.Size;
            Bitmap screenshot = new Bitmap(ssWidth, ssHeight);
            Graphics gfxScreenshot = Graphics.FromImage(screenshot);
            gfxScreenshot.CopyFromScreen(ssX, ssY, 0, 0, ssSize);
            */


            // Save image
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.InitialDirectory = Environment.SpecialFolder.MyDocuments.GetFolderPath();
            dlg.DefaultExt = ".jpg";
            dlg.Filter = "JPEG Images (.jpg)|*.jpg|All files (*.*)|*.*";
            dlg.Title = "Save screenshot..";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                try { screenshot.Save(dlg.FileName, ImageFormat.Jpeg); }
                catch (Exception c) { Console.WriteLine(c.ToString()); }
            }
        }
        #endregion
    }
}
